package com.hippo.drools.utils;

import com.hippo.drools.config.RuleEngineConfig;
import com.hippo.drools.utils.KieUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

import static com.hippo.drools.config.RuleEngineConfig.BASE_RULES_PATH;

/**
 * @ClassName ReloadDroolsRulesServiceImpl
 * @Description TODO 重载规则引擎的规则
 * @Author tangxl
 * @create 2023-05-08 10:49
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class ReloadDroolsRules {
    /**
     * 规则引擎会话
     */
    private final KieSession kieSesion;
    /**
     * 规则引擎容器
     */
    private final KieContainer kieContainer;

    /**
     * 重载规则引擎的规则
     * @param drlName 规则文件名
     */
    public void reload(String drlName) {
        log.info("开始重载规则引擎的规则");
        // 获取规则引擎的文件系统
        KieFileSystem kfs = KieUtils.getKieServices().newKieFileSystem();
        loadFileRules(drlName, kfs);
        kieContainer.updateToVersion(kieContainer.getReleaseId());
        log.info("重载规则引擎的规则结束");
    }

    /**
     * 重新配置规则文件
     * @param drlName 规则文件名
     * @param kfs 规则引擎的文件系统
     */
    private void loadFileRules(String drlName, KieFileSystem kfs) {
        String path = "../../study-commons/drools/src/main/resources/" + RuleEngineConfig.RULES_PATH + drlName + ".drl";
        // 从数据库加载的规则
        kfs.write(path, "\"package plausibcheck.adress\\n\\n import com.leopard.drools.pojo.QueryParam;\\n\\n rule \\\"Postcode 6 numbers\\\"\\n\\n    when\\n  then\\n        System.out.println(\\\"打印日志：更新rules成功!\\\");\\n end\"");
    }

    private Resource[] getRuleFiles(String drlName) throws IOException {
        if (StringUtils.isEmpty(drlName)) {
            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
            return resourcePatternResolver.getResources(BASE_RULES_PATH + RuleEngineConfig.RULES_PATH + "**/*.*");
        }
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        return resourcePatternResolver.getResources(BASE_RULES_PATH + RuleEngineConfig.RULES_PATH + "**/" + drlName + ".*");
    }

    public static void main(String[] args) {
        String path = "study-commons/drools/src/main/resources/" + RuleEngineConfig.RULES_PATH  + "xxx.drl";
        File file = new File("study-commons/drools/src/main/resources/" + RuleEngineConfig.RULES_PATH  + "xxx.drl");
        System.out.println(file.getAbsolutePath());
        // 从数据库加载的规则
        KieFileSystem kfs = KieUtils.getKieServices().newKieFileSystem();
        kfs.write(path, "\"package plausibcheck.adress\\n\\n import com.leopard.drools.pojo.QueryParam;\\n\\n rule \\\"Postcode 6 numbers\\\"\\n\\n    when\\n  then\\n        System.out.println(\\\"打印日志：更新rules成功!\\\");\\n end\"");
    }

}
